#!/usr/bin/python3
#
# RSSNSA RSS nice shim anonymizer (actually it is just a proxy but i want that dope NSA in the name)
# Anonymize your RSS readers by spreading fetches over multiple circuits!
#
# Dependencies:
# * Tor
# * torsocks
# * wget
# * python3


import time
import threading
import random
import subprocess
import http.server
import socketserver

# Fakeuseragent fakes the user agent. Duh. Here are some ideas:
#
# QuiteRSS: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/602.1 (KHTML, like Gecko) QuiteRSS/0.18.12 Safari/602.1
# Akregator: Akregator/5.9.3; syndication
# Liferea: Liferea/1.12.6 (Linux; https://lzone.de/liferea/) AppleWebKit (KHTML, like Gecko)
# newsboat: newsboat/2.13.0 (Linux x86_64)
# FeedReader: FeedReader 2.7.1
# DankShit: Hi, don't mind me. Im just a Tor user. You should host an onion btw!
# Wget: Wget/1.20.1 (linux-gnu)

torip='127.0.0.1'
torport='9050'
randomize=True
fakeuseragent='Hi, don\'t mind me, im just a Tor user. You should host an onion btw!'
serveport=8000

# Format is: ('URL','name on disk/proxyserver',interval in seconds),
# Interval has to be larger than 2 when enabling randomization or tits go up
#
# Seconds - Hours
# 0.1   - 600
# 0.5   - 1800
# 1     - 3600
# 2     - 7200
# 6     - 21600
# 24    - 86400

feeds=[ ('https://blog.torproject.org/rss.xml','torblog.xml',20),
        ('http://tv54samlti22655ohq3oaswm64cwf7ulp6wzkjcvdla2hagqcu7uokid.onion/index.rss','matt.xml',20),
        ('https://www.nsa.gov/DesktopModules/ArticleCS/RSS.ashx?ContentType=1&Site=920&max=20','Fuckers.xml',86400)]

class fetch (threading.Thread):
    def __init__(self, url, file,interval):
        threading.Thread.__init__(self)
        self.url = url
        self.file = file
        self.interval = interval
    
    def run(self):
        while (True):
            subprocess.run(['torsocks','--address',torip,'--port',torport,'-i','wget','--user-agent='+fakeuseragent,'-O'+self.file,self.url])
            if (randomize):
                time.sleep(random.randrange(self.interval/2,self.interval*2))
            else:
                time.sleep(self.interval)


threads = list()

for (url, file, interval) in feeds:
    threads.append(fetch(url,file,interval))

for thread in threads:
    print('Starting thread')
    thread.start()

socketserver.allow_reuse_address = True # Has no effect. lol. Anybody fix me pls.
handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", serveport), handler) as httpd:
    print('Server started at localhost:' + str(serveport))
    socketserver.allow_reuse_address = True
    httpd.serve_forever()



